let express=require(`express`)
let router=express.Router()
let db=require(`../util/db`)
let jwk=require(`jsonwebtoken`)
let {md5}=require(`utility`)



// console.log(Buffer)

//  注册

router.post(`/api/reguser`,(req,res)=>{
    let {password,username}=req.body
        // if(!/^[a-zA-Z][a-zA-Z0-9_]{1,10}/.test(username||password)){
        //     res.send(`用户名或密码格式有误`)
        //     console.log(1111)
        //     return
        // }
        console.log(password,username)
        db(`select * from user where username='${username}'`,(err,result)=>{
            if(err) return res.send({code:1,msg:`sorry`})
            if(result.length>0){
                res.send({code:1,msg:`用户名已存在`})
            }
            else{
                password=md5(password)
                db(`insert into user set username='${username}',password='${password}',nickname='88',email='185@qq.com';`,(err,result)=>{
                    console.log(result)
                    if(err||res.affectedRows===0) return res.send({code:1,msg:`sorry`})
                    res.send({code:0,msg:`注册成功`})
                })
            }
        })
})
// 登录
router.post(`/api/login`,(req,res)=>{
    let {username,password}=req.body
    // let token=
    password=md5(password)
    db(`select *from user where username="${username}" and password="${password}"`,(err,result)=>{
        if(err||result.length===0) return res.send({code:1,msg:"密码或用户名不正确"})
            let token=jwk.sign({id:result[0].id},`hehe`,{expiresIn:'1h'})
        console.log(token)
        res.send({code:200,msg:`登录成功`,token:'Bearer '+token})
    })
})
// 获取用户的基本信息
router.get(`/my/userinfo`,(req,res)=>{
    let {username}=req.query
    // console.log(username)
    db(`select * from user where username="${username}"`,(err,result)=>{
        if(err||result.length===0) return res.send({code:1,msg:"该用户名不存在"})
        result=result[0]
        res.send({code:0,msg:"获取用户基本信息成功",data:{'id':result.id,'username':result.username,'nickname':result.nickname,'email':result.email,'user_pic':result.user_pic}})
        // res.send(result)
    })
})
// 更换用户的基本信息
router.post(`/my/userinfo`,(req,res)=>{
    const{nickname,email}=req.body
    // console.log(nickname,email)
    let{id}=req.user
    console.log(id);
db(`update user set nickname='${nickname}',email='${email}' where id=${id};`,(err,result)=>{
    if(err||result.changedRows===0) return res.send({code:1,msg:'没有修改的地方'})
    res.send({code:0,msg:'修改成功'})
})
})
// 重置密码
router.post(`/my/updatepwd`,(req,res)=>{
    let {id}=req.user
    let {oldPwd}=req.body
    let {newPwd}=req.body
    oldPwd=md5(oldPwd)
    newPwd=md5(newPwd)
    // console.log(password)
    db(`select * from user where password="${oldPwd}" and id=${id}`,(err,result)=>{
        if(err||result.length===0) return res.send({code:1,msg:'密码不正确'})
           db(`update user set password='${newPwd}' where id=${id};`,(err,result)=>{
               console.log(err)
        if(err||result.changedRows===0) return res.send({code:1,msg:'sorry'})
        res.send({code:0,msg:'修改成功'})
            })
    })
 
})
// 更换头像
let Buffer=require(`Buffer`)
router.post(`/my/update/avatar`,(req,res)=>{
let {avatar}=req.body
console.log(44)
let buff=Buffer.from(avatar,'utf-8')
let Base64=buff.toString('base64')
    let {id}=req.user
    console.log(Base64)
    db(`update user set  user_pic="${Base64}" where id= ${id}`,(err,result)=>{
        if(err||result.changedRows===0) return res.send({code:1,msg:'sorry'})
        res.send({code:0,msg:"更换成功"})
    })
})
module.exports=router
